package com.zmovie.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmovie.framework.domain.Order;
import com.zmovie.framework.service.OrderService;
import com.zmovie.framework.mapper.OrderMapper;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author lenovo
* @description 针对表【order】的数据库操作Service实现
* @createDate 2023-12-06 16:08:34
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{
    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public Page<Order> pageOrder(Integer pageNo, Integer pageSize, Order order) {
        Page<Order> orderPage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        //订单状态
        wrapper.eq(order.getState() != null, Order::getState, order.getState());
        //订票日期
        Date date = order.getDate();
        if(date != null){
            String str = format.format(date);
            wrapper.between(Order::getDate, str + " 00:00:00", str + " 23:59:59");
        }
        return page(orderPage, wrapper);
    }

    @Override
    public List<Order> listOrderByUserId(Integer userId) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getUserId, userId);
        wrapper.orderByDesc(Order::getDate);
        return list(wrapper);
    }
}




